/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml.form;

import java.util.LinkedList;
import java.util.List;

import pikes.html.xhtml.Inline;
import pikes.html.xhtml.PCData;
import pikes.util.Assert;
import pikes.xml.XMLTag;

/**
 * <code>&lt;option/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">TABLE
 * element</a> in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">The TABLE element in HTML specification</a>
 */
public class Option extends FormControl {

	private CharSequence value = null;

	private List<Inline> inlines = new LinkedList<Inline>();

	private boolean isSelected = false;

	public Option() {
		// left intentionally empty
	}

	public Option(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public Option(Inline inline) {
		Assert.notNull(inline);
		inlines.add(inline);
	}

	public Option(Inline inline, boolean isSelected) {
		this(inline);
		this.isSelected = isSelected;
	}

	public Option(CharSequence value) {
		Assert.notNull(value);
		this.value = value;
	}

	public Option(CharSequence value, boolean isSelected) {
		this(value);
		this.isSelected = isSelected;
	}

	public Option(CharSequence value, CharSequence content) {
		this(value, new PCData(content));
	}

	public Option(CharSequence value, Inline content) {
		this(value, content, false);
	}

	public Option(CharSequence value, Inline content, boolean isSelected) {
		this(value);
		inlines.add(content);
		this.isSelected = isSelected;
	}

	public Option(CharSequence value, CharSequence content, boolean isSelected) {
		this(value, new PCData(content), isSelected);
	}

	/**
	 * @return "option"
	 */
	@Override
	protected CharSequence getTagName() {
		return "option";
	}

	@Override
	protected void decorateCreatedTag(XMLTag tag) {
		if (value != null) {
			tag.addAttribute("value", value);
		}

		if (isSelected) {
			tag.addAttribute("selected", "selected");
		}

		for (Inline inline : inlines) {
			inline.decorateTag(tag);
		}
	}

	public CharSequence getValue() {
		return value;
	}

	public void setValue(CharSequence value) {
		this.value = value;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

}
